<?php

class mimiweddingController extends stadmincontroller {

    public function __construct($config) {
        parent::__construct($config);
    }

    protected function addSubMenu() {
        parent::addSubMenu();
        add_submenu_page($this->config ['theme_name'], 'SlopeTracker Menus', 'Theme Menus', 'edit_theme_options', 'nav-menus.php');
    }

    public function loadCss() { // load more css custom here
        echo '<link type="text/css" rel="stylesheet" href="' . $this->config['cssUrl'] . 'prettyphoto/prettyPhoto.css">';
    }

    public function loadScript() { // load more js custom here
        echo '<script type="text/javascript" src="' . $this->config ['scriptUrl'] . 'jquery.prettyPhoto.js"></script>';
    }

    public function activationTheme() {
        
    }

    public function index() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'General Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
            $upload_data = wp_upload_dir();

            $data['show_title'] = isset($data['show_title']) ? 1 : 0;
            if (substr($data['headerimg'], 0, 4) != 'http')
                $data['headerimg'] = $upload_data['url'] . '/' . basename($data['headerimg']);

            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function game() {

        $game = $this->loadmodel("game");

        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            if ($game->deleteRecords($_REQUEST['id'])) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Delete successful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Delete unsuccessful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
        }

        $args = array();

        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        }
        $args['limit'] = array($start, $limit);

        $results = $game->getRecords($args);
        $total = $game->countRecords();

        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'name' => __('Name', $this->config['plugin_name']),
            'banner' => __('Banner', $this->config['plugin_name']),
            'thele_image ' => __('Thể lệ', $this->config['plugin_name']),
            'ketqua_image  ' => __('Kết quả', $this->config['plugin_name']),
            'is_top' => __('Status', $this->config['plugin_name']),
        );
        register_column_headers('sttheme_sttheme', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Game list';
        $this->view->iconNew = 'Add Game';
        $this->view->viewNew = 'gameEdit';
    }

    public function gameEdit() {

        $game = $this->loadmodel("game");
        $results = '';
        if (isset($_REQUEST['id'])) {
            $id = $_REQUEST['id'];
            $args = array();
            $args['where'] = " `id` = $id";
            $results = $game->getRecords($args);
        }
        $result = $results[0];
        //initialize variable to view
        $this->view->result = $result;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'New Photo';
        $this->view->headTitle2 = 'Edit Photo';

        // Handle post
        if ($_POST['data']) {
            echo $game->savegame($_POST['data']);
        }
    }
    
    public function chonhinhl1() {

        $chonhinhl1 = $this->loadmodel("chonhinhl1");

        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            if ($chonhinhl1->deleteRecords($_REQUEST['id'])) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Delete successful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Delete unsuccessful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
        }
       
        $args = array();

         // filter by user 
        
        if ($_REQUEST['find_user']) {
            $key = $_REQUEST['find_user'];
            $args['where'] = "`user_id` = '$key'";
        }
        
        //others 
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;
        
        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        }
        $args['limit'] = array($start, $limit);
        
        $results = $chonhinhl1->getRecords($args);
        $total = $chonhinhl1->countRecords();

        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'image' => __('Image', $this->config['plugin_name']),
            'user_id' => __('User', $this->config['plugin_name']),
            'type ' => __('Type', $this->config['plugin_name']),
            'status' => __('Status', $this->config['plugin_name']),
        );
        register_column_headers('sttheme_sttheme', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Game list';
        $this->view->iconNew = 'Add Game';
        $this->view->viewNew = 'gameEdit';
    }


}

?>